package com.whfc.wxmp.api.fmam.controller;

import com.whfc.common.result.*;
import com.whfc.common.util.JSONUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fmam.entity.dto.*;
import com.whfc.fmam.entity.enums.WeightNoteSourceType;
import com.whfc.fmam.entity.qo.WeighNoteAddQO;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.fmam.service.WxFmamWeighNoteService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * 物资-磅单
 *
 * @author : qzexing
 * @version : 1.0
 * @date : 2020/4/8 23:11
 */
@RestController
@RequestMapping("/mp/api/fmam/weighNote")
public class WxFmamWeighNoteController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxFmamWeighNoteService wxWeighNoteService;

    @RequestMapping("/mobile/list")
    public Result mobileList(@CurrentWxUser WxUser wxUser,
                             @RequestParam(value = "weightNoteType", required = false) Integer weightNoteType,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                             @RequestParam(value = "code", required = false) String code,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取磅单列表", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.list(weightNoteType, deptId, date, code, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/mobile/detail")
    public Result mobileDetail(@CurrentWxUser WxUser wxUser,
                               @RequestParam("id") Long id) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取磅单详情", "{\"id\":\"" + id + "\"}");
        FmamWeighNoteDetailDTO weighNoteDetailDTO = wxWeighNoteService.detail(id);
        return ResultUtil.success(weighNoteDetailDTO);
    }


    @GetMapping("/mobile/total")
    public Result total(@CurrentWxUser WxUser wxUser,
                        @RequestParam(value = "date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                        @RequestParam(value = "deptId") Integer deptId,
                        @RequestParam(value = "code", required = false) String code) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取磅单数量", RequestConetxtUtil.getParameters());
        FmamWeighNoteTotalDTO weighNoteTotalDTO = wxWeighNoteService.getWeighNoteTotal(deptId, date, code);
        return ResultUtil.success(weighNoteTotalDTO);
    }

    @PostMapping("/mobile/add")
    public Result add(@CurrentWxUser WxUser wxUser,
                      @RequestBody @Validated WeighNoteAddQO request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "新增磅单", JSONUtil.toString(request));
        request.setSource(WeightNoteSourceType.CHECK.getValue().toString());
        wxWeighNoteService.add(wxUser, request);
        return ResultUtil.success();
    }


    @PostMapping("/mobile/uploadImg")
    public Result uploadImg(@CurrentWxUser WxUser wxUser,
                            @RequestParam("imgType") Integer imgType,
                            @RequestParam("file") MultipartFile file) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "上传物资图片磅单", "{\"imgType\":\"" + imgType + "\",\"fileName\":\"" + file.getOriginalFilename() + "\"}");
        FmamWeighNoteImgDTO weighNoteImgDTO = wxWeighNoteService.uploadImg(imgType, file);
        return ResultUtil.success(weighNoteImgDTO);
    }

    @GetMapping("/count")
    public Result count(@CurrentWxUser WxUser wxUser,
                        @RequestParam(value = "deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "统计磅单数量", "{\"deptId\":\"" + deptId + "\"}");
        FmamWeighNoteCountDTO weighNoteCountDTO = wxWeighNoteService.weighNoteCount(deptId);
        return ResultUtil.success(weighNoteCountDTO);
    }

    @GetMapping(value = "/count/recvSendType", params = "date")
    public Result countRecvSendTypeByDate(@CurrentWxUser WxUser wxUser,
                                          @RequestParam(value = "deptId") Integer deptId,
                                          @RequestParam(value = "date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "统计收发料类型数量-日统计", RequestConetxtUtil.getParameters());
        FmamWeighNoteTotalDTO weighNoteTotalDTO = wxWeighNoteService.recvSendTypeTotalByDay(deptId, date);
        return ResultUtil.success(weighNoteTotalDTO);
    }

    @GetMapping(value = "/count/recvSendType", params = "month")
    public Result countRecvSendTypeByMonth(@CurrentWxUser WxUser wxUser,
                                           @RequestParam(value = "deptId") Integer deptId,
                                           @RequestParam(value = "month") @DateTimeFormat(pattern = "yyyy-MM") Date month) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "统计收发料类型数量-月统计", RequestConetxtUtil.getParameters());
        FmamWeighNoteTotalDTO weighNoteTotalDTO = wxWeighNoteService.recvSendTypeTotalByMonth(deptId, month);
        return ResultUtil.success(weighNoteTotalDTO);
    }

    @GetMapping(value = "/count/recvSendType")
    public Result countRecvSendType(@CurrentWxUser WxUser wxUser,
                                    @RequestParam(value = "deptId") Integer deptId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "统计收发料类型数量-累计统计", "{\"deptId\":\"" + deptId + "\"}");
        FmamWeighNoteTotalDTO weighNoteTotalDTO = wxWeighNoteService.recvSendTypeTotal(deptId);
        return ResultUtil.success(weighNoteTotalDTO);
    }


    @GetMapping(value = "/count/recvSendType/v2")
    public Result countRecvSendTypeByMonthv2(@CurrentWxUser WxUser wxUser,
                                             @RequestParam(value = "deptId") Integer deptId,
                                             @RequestParam(value = "source", required = false) Integer source,
                                             @RequestParam(value = "dateType") Integer dateType,
                                             @RequestParam(value = "date", required = false)  String date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "统计收发料类型数量-v2", RequestConetxtUtil.getParameters());
        FmamWeighNoteTotalDTO weighNoteTotalDTO = wxWeighNoteService.recvSendTypeTotalV2(deptId, date,dateType,source);
        return ResultUtil.success(weighNoteTotalDTO);
    }

    @GetMapping("/detail")
    public Result detail(@CurrentWxUser WxUser wxUser,
                         @RequestParam("id") Long id) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}", wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(), "获取磅单详情", "{\"id\":\"" + id + "\"}");
        FmamWeighNoteDetailDTO weighNoteDetailDTO = wxWeighNoteService.detail(id);
        return ResultUtil.success(weighNoteDetailDTO);
    }


    @GetMapping(value = "/list", params = "date")
    public Result listByDate(@CurrentWxUser WxUser wxUser,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                             @RequestParam(value = "weighNoteType", required = false) Integer weighNoteType,
                             @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                             @RequestParam(value = "source", required = false) Integer source,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("pageNum") Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-获取磅单列表-每日", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.listByRecvSendTypeAndDay(deptId, date, weighNoteType, recvSendType, pageSize, pageNum,source);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list", params = "month")
    public Result listByMonth(@CurrentWxUser WxUser wxUser,
                              @RequestParam("deptId") Integer deptId,
                              @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month,
                              @RequestParam(value = "weighNoteType", required = false) Integer weighNoteType,
                              @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                              @RequestParam(value = "source", required = false) Integer source,
                              @RequestParam("pageSize") Integer pageSize,
                              @RequestParam("pageNum") Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-获取磅单列表-每月", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.listByRecvSendTypeAndMonth(deptId, month, weighNoteType, recvSendType, pageSize, pageNum,source);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list")
    public Result list(@CurrentWxUser WxUser wxUser,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "weighNoteType", required = false) Integer weighNoteType,
                       @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                       @RequestParam(value = "source", required = false) Integer source,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNum") Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-获取磅单列表-累计", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.listByRecvSendType(deptId, weighNoteType, recvSendType, pageSize, pageNum,source);
        return ResultUtil.success(pageData);
    }


    @GetMapping(value = "/analysis", params = "date")
    public Result analysisByDate(@CurrentWxUser WxUser wxUser,
                                 @RequestParam("deptId") Integer deptId,
                                 @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                 @RequestParam(value = "weighNoteType") Integer weighNoteType,
                                 @RequestParam(value = "source", required = false) Integer source,
                                 @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                                 @RequestParam(value = "matTypeId", required = false) Long matTypeId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析-每日", RequestConetxtUtil.getParameters());
        ListData<FmamWeighNoteAnalysisDTO> list = wxWeighNoteService.analysisByDate(deptId, date, weighNoteType, source, recvSendType, matTypeId);
        return ResultUtil.success(list);
    }

    @GetMapping(value = "/analysis", params = "month")
    public Result analysisByMonth(@CurrentWxUser WxUser wxUser,
                                  @RequestParam("deptId") Integer deptId,
                                  @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month,
                                  @RequestParam(value = "weighNoteType") Integer weighNoteType,
                                  @RequestParam(value = "source", required = false) Integer source,
                                  @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                                  @RequestParam(value = "matTypeId", required = false) Long matTypeId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析-每月", RequestConetxtUtil.getParameters());
        ListData<FmamWeighNoteAnalysisDTO> list = wxWeighNoteService.analysisByMonth(deptId, month, weighNoteType, source, recvSendType, matTypeId);
        return ResultUtil.success(list);
    }


    @GetMapping(value = "/analysis")
    public Result analysis(@CurrentWxUser WxUser wxUser,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam(value = "weighNoteType") Integer weighNoteType,
                           @RequestParam(value = "source", required = false) Integer source,
                           @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                           @RequestParam(value = "matTypeId", required = false) Long matTypeId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析-累计", RequestConetxtUtil.getParameters());
        ListData<FmamWeighNoteAnalysisDTO> list = wxWeighNoteService.analysis(deptId, weighNoteType, source, recvSendType, matTypeId);
        return ResultUtil.success(list);
    }

    @GetMapping(value = "/analysis/v2")
    public R<FmamRecvSendAnaDTO> analysisV2(@CurrentWxUser WxUser wxUser,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam(value = "weighNoteType") Integer weighNoteType,
                           @RequestParam(value = "source", required = false) Integer source,
                           @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                           @RequestParam(value = "dateType", required = false) Integer dateType,
                           @RequestParam(value = "date", required = false)  String date,
                           @RequestParam(value = "matTypeId", required = false) Long matTypeId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析-累计", RequestConetxtUtil.getParameters());
        FmamRecvSendAnaDTO list = wxWeighNoteService.analysisV2(deptId, weighNoteType, source, recvSendType, matTypeId,date,dateType);
        return R.success(list);
    }

    @GetMapping(value = "/analysis/list/v2")
    public R<FmamRecvSendAnaDTO> analysisListV2(@CurrentWxUser WxUser wxUser,
                                                @RequestParam("deptId") Integer deptId,
                                                @RequestParam("matTypeId") Long matTypeId,
                                                @RequestParam(value = "dateType", required = false) Integer dateType,
                                                @RequestParam(value = "code", required = false) String code,
                                                @RequestParam(value = "date", required = false) String date,
                                                @RequestParam(value = "weighNoteType") Integer weighNoteType,
                                                @RequestParam(value = "source", required = false) Integer source,
                                                @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                                                @RequestParam(value = "pageNum") Integer pageNum,
                                                @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析列表", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> list = wxWeighNoteService.analysisListV2(deptId, matTypeId,  weighNoteType, recvSendType, code, source, pageSize, pageNum,dateType,date);
        return R.success(list);
    }


    @GetMapping(value = "/analysis/list", params = "date")
    public Result analysisListByDate(@CurrentWxUser WxUser wxUser,
                                     @RequestParam("deptId") Integer deptId,
                                     @RequestParam("matId") Long matId,
                                     @RequestParam(value = "code", required = false) String code,
                                     @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                     @RequestParam(value = "weighNoteType") Integer weighNoteType,
                                     @RequestParam(value = "source", required = false) Integer source,
                                     @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                                     @RequestParam(value = "pageNum") Integer pageNum,
                                     @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析列表-每日", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.analysisListByDate(deptId, matId, date, weighNoteType, recvSendType, code, source, pageSize, pageNum);
        return ResultUtil.success(pageData);
    }


    @GetMapping(value = "/analysis/list", params = "month")
    public Result analysisListByMonth(@CurrentWxUser WxUser wxUser,
                                      @RequestParam("deptId") Integer deptId,
                                      @RequestParam("matId") Long matId,
                                      @RequestParam(value = "code", required = false) String code,
                                      @RequestParam("month") @DateTimeFormat(pattern = "yyyy-MM") Date month,
                                      @RequestParam(value = "weighNoteType") Integer weighNoteType,
                                      @RequestParam(value = "source", required = false) Integer source,
                                      @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                                      @RequestParam(value = "pageNum") Integer pageNum,
                                      @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析列表-每月", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.analysisListByMonth(deptId, matId, month, weighNoteType, recvSendType, code, source, pageSize, pageNum);
        return ResultUtil.success(pageData);
    }


    @GetMapping(value = "/analysis/list")
    public Result analysisList(@CurrentWxUser WxUser wxUser,
                               @RequestParam("deptId") Integer deptId,
                               @RequestParam("matId") Long matId,
                               @RequestParam(value = "code", required = false) String code,
                               @RequestParam(value = "weighNoteType") Integer weighNoteType,
                               @RequestParam(value = "source", required = false) Integer source,
                               @RequestParam(value = "recvSendType", required = false) Integer recvSendType,
                               @RequestParam(value = "pageNum") Integer pageNum,
                               @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-磅单材料分析列表-累计", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.analysisList(deptId, matId, weighNoteType, recvSendType, code, source, pageSize, pageNum);
        return ResultUtil.success(pageData);
    }


    @GetMapping(value = "/daily")
    public Result daily(@CurrentWxUser WxUser wxUser,
                        @RequestParam("deptId") Integer deptId,
                        @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") Date time,
                        @RequestParam(value = "weighNoteType") Integer weighNoteType,
                        @RequestParam(value = "pageNum") Integer pageNum,
                        @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "材料进出场-日报", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDailyDTO> pageData = wxWeighNoteService.daily(deptId, time, weighNoteType, pageSize, pageNum);
        return ResultUtil.success(pageData);
    }


    /**
     * 2.0 收发料预警
     * @param deptId
     * @param weighNoteType 磅单类型(1-收料 2-发料)
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/list/warn")
    public Result listByWarn(@CurrentWxUser WxUser wxUser,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam(value = "weighNoteType", required = true) Integer weighNoteType,
                             @RequestParam(value = "pageSize", required = false) Integer pageSize,
                             @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "磅单管理-获取磅单列表-每日", RequestConetxtUtil.getParameters());
        PageData<FmamWeighNoteDTO> pageData = wxWeighNoteService.listWarnByRecvSendType(deptId, weighNoteType, pageSize, pageNum);
        return ResultUtil.success(pageData);
    }
}
